package com.xingkeduo.pojo.json;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.dto.Hairstyle.HairstyleDTO;
import com.xingkeduo.pojo.query.HairstyleQuery;
import com.xingkeduo.pojo.table.HairstyleTable;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.base.BaseTable;
import com.xingkeduo.util.pojo.domain.Page;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;

public class HairstyleJson extends BaseJson {

	//查询
	public static HairstyleQuery createQueryForHairstyle(String json) throws Throwable {
		HairstyleQuery query = new HairstyleQuery();
		JsonNode root = validJson(json);
		if (root == null)
			return query;
		query.setId(validId(root, YesNoEnum.YES));
		return query;
	}

	//修改
	public static HairstyleDTO createTableForModifyHairstyle(String urlParam, HairstyleDTO hairstyleDTO) throws Throwable {
		JsonNode root = validJson(urlParam);
		if (root == null)
			throw new InputParamException(InputParamException.Type.INVALID, "参数");
        hairstyleDTO.setId(validId(root, YesNoEnum.YES));

		return hairstyleDTO;
	}

	public static ObjectNode toJson(HairstyleTable hairstyleTable) {
		ObjectNode root = MAPPER.createObjectNode();

		if (hairstyleTable != null)
		{
			root.put("id", hairstyleTable.getId());
			root.put("code", hairstyleTable.getCode());
			root.put("name", hairstyleTable.getName());
            root.put("enmark", hairstyleTable.getEnmark());
            root.put("sex", hairstyleTable.getSex());
            root.put("age", hairstyleTable.getAge());
            root.put("status", hairstyleTable.getStatus());
            root.put("servprodIds", JSON.toJSONString(hairstyleTable.getServprodIds()));
            root.put("status", hairstyleTable.getStatus());
            root.put("styleTagids", hairstyleTable.getStyleTagids());
            root.put("faceTagids", hairstyleTable.getFaceTagids());
            root.put("jobTagids", hairstyleTable.getJobTagids());
            root.put("title", hairstyleTable.getTitle());
            root.put("cover", hairstyleTable.getCover());
            root.put("picIds", hairstyleTable.getPicIds());
            root.put("anatomyIds", hairstyleTable.getAnatomyIds());
            root.put("intro", hairstyleTable.getIntro());
            root.put("imgDir", hairstyleTable.getImgDir());
            root.put("likeNum", hairstyleTable.getLikeNum());
            root.put("createdBy", hairstyleTable.getCreatedBy());
			//处理时间
			String createdTime =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(hairstyleTable.getCreatedTime());
			root.put("createtime", createdTime);

            root.put("updatedBy", hairstyleTable.getUpdatedBy());
			String updatedTime =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(hairstyleTable.getUpdatedTime());
            root.put("updatedTime", updatedTime);
		}

		return root;
	}

	public static ArrayNode toJson(List<HairstyleTable> hairstyleTableList) throws Throwable {
		ArrayNode root = MAPPER.createArrayNode();

		if(!CommonUtil.isEmpty(hairstyleTableList)){
			for (Iterator<HairstyleTable> iterator = hairstyleTableList.iterator(); iterator.hasNext();) {
				root.add(toJson(iterator.next()));
			}
		}

		return root;
	}

	public static ObjectNode toJsonPage(Page<HairstyleDTO> page) throws Throwable {
		ObjectNode root = MAPPER.createObjectNode();
		if (page != null)
		{
			root.put("pageNo", page.getPageNo());
			root.put("pageSize", page.getPageSize());
			root.put("pageTotal", page.getPageTotal());
			root.put("recordTotal", page.getRecordTotal());
			root.put("dtoLists", JSON.toJSONString(page.getDtoList()));
		}
		return root;
	}
}
